import { ref } from 'vue'
import { defineStore, storeToRefs } from 'pinia'

import { type PageInfo } from '@/config/types'
import api from '@/api/student'
import apiLesson from '@/api/lesson'
import cfg from '@/config/config'
import type {
  MyLessonListItem,
  MyChapterItem,
  EbookItem,
  LessonDetail,
  VideoSource
} from '@/config/types'

export const useStudentLessonStore = defineStore('studentLessonStore', () => {
  const loading = ref<boolean>(false)
  const lessonList = ref<MyLessonListItem[]>([])
  const pageInfo = ref<PageInfo>({
    page: 1,
    limit: cfg.pageSize,
    total: 0
  })

  const lessonDetail = ref<LessonDetail>({
    id: 0,
    title: '',
    thumb: '',
    categoryId: 0,
    categoryTitle: '',
    price: 0,
    marketPrice: 0,
    summary: '',
    content: '',
    teachers: '',
    isBuyed: false
  })
  const chapterList = ref<MyChapterItem[]>([])
  const ebookList = ref<EbookItem[]>([])
  const video = ref<VideoSource>({
    id: 0,
    src: '',
    type: 'video/mp4'
  })

  async function getMyLessons(expired: boolean) {
    loading.value = true
    try {
      const res = await api.myLessons(expired, pageInfo.value)
      lessonList.value = res.data
      pageInfo.value = {
        page: res.page,
        limit: res.size,
        total: res.total
      }
    } catch (e) {
      lessonList.value = []
    } finally {
      loading.value = false
    }
  }

  function changePage(page: number, expired: boolean) {
    pageInfo.value.page = page
    getMyLessons(expired)
  }

  function switchLessons(expired: boolean) {
    pageInfo.value.page = 1
    getMyLessons(expired)
  }

  async function loadDetail(id: number) {
    loading.value = true
    try {
      const res = await apiLesson.detail(id)
      lessonDetail.value = res.data
    } finally {
      loading.value = false
    }
  }

  async function getChapters(id: number) {
    loading.value = true
    try {
      const res = await api.lessonChapters(id)
      chapterList.value = res.data
    } catch (e) {
      chapterList.value = []
    } finally {
      loading.value = false
    }
  }

  async function getEbooks(id: number) {
    loading.value = true
    try {
      const res = await api.lessonEbooks(id)
      ebookList.value = res.data
    } catch (e) {
      ebookList.value = []
    } finally {
      loading.value = false
    }
  }

  function playVideo(chapterId: number) {
    if (chapterId == 0 && chapterList.value.length > 0) {
      chapterList.value[0].play = true
      video.value.id = chapterList.value[0].id
      video.value.src = cfg.videoUrl(chapterList.value[0].video)
      return
    }
    chapterList.value.forEach((item: MyChapterItem) => {
      if (item.id == chapterId) {
        video.value.id = item.id
        video.value.src = cfg.videoUrl(item.video)
        item.play = true
      } else {
        item.play = false
      }
    })
  }

  return {
    loading,
    pageInfo,
    lessonList,
    lessonDetail,
    chapterList,
    ebookList,
    video,

    getMyLessons,
    switchLessons,
    loadDetail,
    getChapters,
    getEbooks,
    playVideo
  }
})
